package flst.ipiweb.struts.action.article;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import flst.ipiweb.service.ServiceArticle;
import flst.ipiweb.transverse.Article;
import flst.ipiweb.transverse.IPIException;

public class DeleteArticleAction extends Action{

	public ActionForward execute(
			ActionMapping mapping, 
			ActionForm form,
			HttpServletRequest request, 
			HttpServletResponse response)
			throws Exception {
		
			//On récupère la session
			HttpSession session = request.getSession();
			//Si l'administrateur n'est pas logué 
			//on le redirige sur le formulaire de login
			if(session.getAttribute("login") == null)
			{
				return mapping.findForward("login");
			}
			
			
			Article article = new Article();
			//Récupération de l'id de l'article à supprimer
			int idArticle = Integer.parseInt(request.getParameter("id"));
			article.setIdArticle(idArticle);
			
			//Instanciation du service article
			ServiceArticle sArticle = new ServiceArticle();
			try
			{
				//Suppression de l'article
				sArticle.deleteArticle(article);
				session.setAttribute("success","ArticleSuccess.delete");
				session.setAttribute("error", null);
			}
			catch(IPIException e)
			{
				session.setAttribute("success",null);
				session.setAttribute("error", e.getMessage());
			}
	
			return mapping.findForward("list_article");
		}
}
